package com.jysz.webcore.ssh.web.struts2;

import net.sf.json.JSONObject;

import com.jysz.webcore.ssh.orm.hibernate.support.JsonWrapper;
import com.jysz.webcore.utils.SshStringUtils;

/**
 * @author kacarich email:javaengine@outlook.com
 *
 */
public class SshAction extends GenericAction {

	private static final long serialVersionUID = 1L;
	protected static final String PERSIST = "persist";
	protected static final String FORM = "form";
	protected static final String ADD = "add";
	protected static final String EDIT = "edit";
	protected static final String LIST = "list";
	protected static final String INDEX = "index";
	protected static final String CREATE = "create";
	protected static final String UPDATE = "update";
	protected static final String DESTORY = "destory";
	protected static final String PAGE_QUERY = "pageQuery";
	protected static final String REDIRECT_INDEX = "redirectIndex";
	protected static final String REDIRECT_FORM = "redirectForm";
	protected static final String SHOW = "show";
	

	protected static final String UNKNOWN_MSG = "服务器内部错误";

	public static final String JSON_PAGE = "jsonPage";

	public static final String REJECT_PAGE = "rejectPage";

	public static final String ERROR_PAGE = "errorPage";

	public static final String NEED_PERMISSION_PAGE = "permissionPage";

	public static final String TIME_OUT_PAGE = "timeoutPage";

	public static final String LOGIN_PAGE = "loginPage";
	
	public static final String SHOW_MSG_PAGE = "showMsgPage";
	
	
	public static final String USER = "_user";

	/**
	 * 验证码标识
	 */
	public static final String AUTH_CODE = "authCode";

	private String popMessage = null; // 标准提示信息
	private boolean resultBoolean = true; // 成功失败标志
	private boolean timeout;
	private String exceptionMessage; // 异常信息

	@Override
	public String execute() throws Exception {
		super.execute();
		return LIST;
	}

	public String getPopMessage() {
		return popMessage;
	}

	public void setPopMessage(String popMessage) {
		this.popMessage = popMessage;
	}

	public boolean isResultBoolean() {
		return resultBoolean;
	}

	public void setResultBoolean(boolean resultBoolean) {
		this.resultBoolean = resultBoolean;
	}

	public void setSuccessPopMessage(String popMessage) {
		this.popMessage = popMessage;
		this.resultBoolean = true;
	}

	public void setFailPopMessage(String popMessage) {
		this.popMessage = popMessage;
		this.resultBoolean = false;
	}

	public String toUtf8Encode(String s) {
		if (s == null)
			return null;
		else
			return SshStringUtils.toUtf8Encode(s);
	}

	public boolean isTimeout() {
		return timeout;
	}

	public void setTimeout(boolean timeout) {
		this.timeout = timeout;
	}

	public void setJsonMsg(Object data) {
		setJsonMsg(popMessage, resultBoolean, data);
	}

	public void setJsonMsg(String msg, boolean flag, Object data) {
		JsonWrapper<Object> wrapper = new JsonWrapper<Object>(flag, msg, data);
		setJsonText(JSONObject.fromObject(wrapper).toString());
	}

	public void setJsonMsg() {
		setJsonMsg(popMessage, resultBoolean, null);
	}

	public void setExceptionMessage(Exception e) {
		StringBuilder sb = new StringBuilder();
		StackTraceElement[] trace = e.getStackTrace();
        for (int i=0; i < trace.length; i++)
        	sb.append(trace[i]+"\r\n");

        Throwable ourCause = e.getCause();
        if (ourCause != null)
        	stackTraceAsCause(sb, trace, e);
		exceptionMessage = sb.toString();
	}
	
	
	private void stackTraceAsCause(StringBuilder sb, StackTraceElement[] causedTrace, Exception e) {
		StackTraceElement[] trace = e.getStackTrace();
		int m = trace.length - 1, n = causedTrace.length - 1;
		while (m >= 0 && n >= 0 && trace[m].equals(causedTrace[n])) {
			m--;
			n--;
		}
		int framesInCommon = trace.length - 1 - m;

		sb.append("Caused by: " + this+"\r\n");
		for (int i = 0; i <= m; i++)
			sb.append(trace[i]+"\r\n");
		if (framesInCommon != 0)
			sb.append("\t... " + framesInCommon + " more"+"\r\n");

		Throwable ourCause = e.getCause();
		if (ourCause != null)
			stackTraceAsCause(sb, trace, e);
	}

	public String getExceptionMessage() {
		return exceptionMessage;
	}

	
}
